from sqlalchemy.orm import Session
from . import models, schemas

def get_phoneapp_version(db: Session, phoneapp_version_id: int):
    return db.query(models.PhoneappVersions).filter(models.PhoneappVersions.Id == phoneapp_version_id).first()

def get_phoneapp_versions(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.PhoneappVersions).offset(skip).limit(limit).all()

def create_phoneapp_version(db: Session, phoneapp_version: schemas.PhoneappVersionsCreate):
    db_phoneapp_version = models.PhoneappVersions(**phoneapp_version.dict())
    db.add(db_phoneapp_version)
    db.commit()
    db.refresh(db_phoneapp_version)
    return db_phoneapp_version

def update_phoneapp_version(db: Session, phoneapp_version_id: int, phoneapp_version: schemas.PhoneappVersionsCreate):
    db_phoneapp_version = db.query(models.PhoneappVersions).filter(models.PhoneappVersions.Id == phoneapp_version_id).first()
    if db_phoneapp_version:
        for key, value in phoneapp_version.dict().items():
            setattr(db_phoneapp_version, key, value)
        db.commit()
        db.refresh(db_phoneapp_version)
    return db_phoneapp_version

def delete_phoneapp_version(db: Session, phoneapp_version_id: int):
    db_phoneapp_version = db.query(models.PhoneappVersions).filter(models.PhoneappVersions.Id == phoneapp_version_id).first()
    if db_phoneapp_version:
        db.delete(db_phoneapp_version)
        db.commit()
    return db_phoneapp_version